﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataWorks_Sqlite
{
    /// <summary>
    /// 当执行迁移时发生如下错误：Unable to create an object of type 'MyDbContext'，需要这个类重新返回一个MyDbContext
    /// </summary>
    public class DbContextDesignTimeFactory : IDesignTimeDbContextFactory<MyDbContext>
    {
        public MyDbContext CreateDbContext(string[] args)
        {
            // 获取 WebAPI 项目的绝对路径（相对当前项目的）
            var basePath = Path.Combine(Directory.GetCurrentDirectory(), "../DataWorks_WebApi_Sqlite");
            var dbPath = Path.GetFullPath(Path.Combine(basePath, "dataworks_sqlite.db"));

            DbContextOptionsBuilder<MyDbContext> builder = new DbContextOptionsBuilder<MyDbContext>();
            builder.UseSqlite($"Data Source={dbPath}");
            return new MyDbContext(builder.Options);
        }
    }
}
